package com.cainaer.shop.controller;


import com.cainaer.common.core.airecognition.Sample;
import com.cainaer.common.core.airecognition.domain.BusinessLicense;
import com.cainaer.common.core.constant.UserConstants;
import com.cainaer.common.core.utils.MobileUtil;
import com.cainaer.common.core.utils.StringUtils;
import com.cainaer.common.core.web.controller.BaseController;
import com.cainaer.common.core.web.domain.AjaxResult;
import com.cainaer.common.redis.service.RedisService;
import com.cainaer.shop.domain.ShopInfo;
import com.cainaer.shop.domain.ShopLogin;
import com.cainaer.shop.domain.vo.BindTelVo;
import com.cainaer.shop.service.IShopInfoService;
import com.cainaer.shop.service.IShopLoginService;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * <p>
 * 商家绑定 前端控制器
 * </p>
 *
 * @author Tina
 * @since 2020-09-03
 */
@RestController
@RequestMapping("/bind/")
public class BindController extends BaseController {


    @Autowired
    private RedisService redisService;

    @Autowired
    private IShopLoginService shopLoginService;

    @Autowired
    private IShopInfoService shopInfoService;

    private Logger logger = LoggerFactory.getLogger(BindController.class);

    @Value("${pathFile.img}")
    private String pathFileImg;

    @Value("${fileTomcat.imgUrl}")
    private String fileTomcatImgUrl;

    /**
     * 绑定手机号码
     *
     * @param bindTelVo
     * @return
     */
    @GetMapping("tel")
    public AjaxResult bindTel(@Validated BindTelVo bindTelVo) {
        logger.info("bindTelVo->{}", bindTelVo);
        boolean mobileNum = MobileUtil.isMobileNum(bindTelVo.getTel());
        if (!mobileNum) {
            return AjaxResult.error("号码不正确");
        }
        String cacheObject = redisService.getCacheObject("smsCode:" + bindTelVo.getTel() + ":" + bindTelVo.getCode());
        if (StringUtils.isEmpty(cacheObject)) {
            return AjaxResult.error("验证码无效");
        }
        String checkPhoneUnique = shopLoginService.checkPhoneUnique(bindTelVo.getTel());
        if (UserConstants.NOT_UNIQUE.equals(checkPhoneUnique)) {
            return AjaxResult.error("号码已被绑定");
        }

        ShopLogin shopLogin = new ShopLogin();
        shopLogin.setShopId(Long.valueOf(bindTelVo.getShopId()));
        shopLogin.setTel(bindTelVo.getTel());
        return toAjax(shopLoginService.editShopLogin(shopLogin));
    }


    /**
     * 上传门店图
     *
     * @param file
     * @return
     */
    @PostMapping("storeImg")
    public AjaxResult businessLicense(@RequestParam("file") MultipartFile file, ShopInfo shopInfo) throws IOException {
        System.out.println("pathFile->" + pathFileImg);
        if (!file.isEmpty()) {
            // 获取文件并将其保存在某处
            String url = pathFileImg + "storeImg/" + shopInfo.getShopId() + "/";
            String fileUrlTomcat = fileTomcatImgUrl + "storeImg/" + shopInfo.getShopId() + "/";
            String myFileOriginalFilename = file.getOriginalFilename();
            FileUtils.copyInputStreamToFile(file.getInputStream(), new File(url, myFileOriginalFilename));

            shopInfo.setStoreImg(fileUrlTomcat + myFileOriginalFilename);
            Integer integer = shopInfoService.editShopInfoByShop(shopInfo);

            return AjaxResult.success(200,fileUrlTomcat + myFileOriginalFilename);
        }
        return AjaxResult.error();
    }


}
